package com.duofan.gmall.coupon.service.impl;

import com.duofan.gmall.common.to.MemberPrice;
import com.duofan.gmall.common.to.SkuReductionTo;
import com.duofan.gmall.coupon.entity.MemberPriceEntity;
import com.duofan.gmall.coupon.entity.SkuLadderEntity;
import com.duofan.gmall.coupon.service.MemberPriceService;
import com.duofan.gmall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.duofan.gmall.common.utils.PageUtils;
import com.duofan.gmall.common.utils.Query;

import com.duofan.gmall.coupon.dao.SkuFullReductionDao;
import com.duofan.gmall.coupon.entity.SkuFullReductionEntity;
import com.duofan.gmall.coupon.service.SkuFullReductionService;
import org.springframework.util.CollectionUtils;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
    @Autowired
    SkuLadderService skuLadderService;
    @Autowired
    MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTo reductionTo) {
        // 1.保存满减打折 sms_sku_ladder \ sms_sku_full_reduction \ sms_member_price
        if(reductionTo.getFullCount() > 0) {
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            skuLadderEntity.setSkuId(reductionTo.getSkuId());
            skuLadderEntity.setFullCount(reductionTo.getFullCount());
            skuLadderEntity.setDiscount(reductionTo.getDiscount());
            skuLadderEntity.setAddOther(reductionTo.getCountStatus());
            skuLadderService.save(skuLadderEntity);
        }


        // 2 sms_sku_full_reduction
        if(reductionTo.getFullPrice().compareTo(new BigDecimal(0)) == 1) {
            SkuFullReductionEntity reductionEntity = new SkuFullReductionEntity();
            BeanUtils.copyProperties(reductionTo, reductionEntity);
            this.save(reductionEntity);
        }

        // 3 sms_member_price
        List<MemberPrice> memberPrices = reductionTo.getMemberPrice();
        if(!CollectionUtils.isEmpty(memberPrices)) {
            List<MemberPriceEntity> priceEntities = memberPrices.stream().map(e -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setSkuId(reductionTo.getSkuId());
                memberPriceEntity.setMemberLevelId(e.getId());
                memberPriceEntity.setMemberLevelName(e.getName());
                memberPriceEntity.setMemberPrice(e.getPrice());
                return memberPriceEntity;
            }).filter(e -> e.getMemberPrice().compareTo(new BigDecimal(0)) == 1)
            .collect(Collectors.toList());
            memberPriceService.saveBatch(priceEntities);
        }
    }

}